//
//  RCRecommendFoodDetailViewController.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-24.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCRecommendFoodDetailViewController.h"
#import "IManager+ModuleRecommendFoodDetail.h"
#import "IManager+ModuleMerchantFavourite.h"
#import "RCRecommendFoodDetailDataModel.h"
#import "RCMerchantDetailCell.h"
#import "RCRecommendFoodDetailHeaderCell.h"
#import <MapKit/MapKit.h>
#import "UIColor-Expanded.h"
#import "CommonDefines.h"
#import "RCWaitingMoreVC.h"
#import "GloableEngineService.h"
#import "AppDelegate.h"
#import "RCMerchantCommentListViewController.h"
#import "RCMerchantDetailTrafficViewController.h"
#import "RCMerchantDetailOtherShopViewController.h"
#import "RCMerchantDetailWriteSigninViewController.h"
#import "RCMerchantCommitWrongInfoViewController.h"
#import "RCMerchantDetailSigninListViewController.h"
#import "RCMerchantDetailWriteCommentViewController.h"
#import "RCMerchantListVC.h"
#import "RCMerchantUploadPhotoViewController.h"
#import "RCMerchantPictureViewController.h"


#define kSignImageFile            @"image_merchant_sign.png"
#define kSendPhotoImageFile       @"image_merchant_sendPhoto.png"
#define kDianpingImageFile        @"image_merchant_dianping.png"
#define kWrongImageFile           @"image_merchant_wrong.png"
#define kShareImageFile           @"image_merchant_share.png"
#define kFavouriteImageFile       @"image_merchant_favourite.png"
#define kFavouritedImageFile       @"image_merchant_favourited.png"
#define kBottomBarImageFile       @"image_merchant_bottom_bar.png"

@interface RCRecommendFoodDetailViewController ()<IManagerDelegate,UITableViewDataSource,UITableViewDelegate,MerchantWriteSigninDelegate,MerchantWriteCommentDelegate,RCMerchantPeripheryCellDelegte,RCRecommendFoodHeaderCellDelegate>{
    UITableView *_tableView;
    RCRecommendFoodDetailDataModel *_dataModel;
}
@property (nonatomic, copy) NSString *foodID;
@property (nonatomic, retain) RCRecommendFoodDetailDataModel *dataModel;
@property (nonatomic, assign) int64_t requestID;
@property (nonatomic, assign) int64_t favouriterRequestID;
@property (nonatomic, assign) int64_t cancelRequestID;
@property (nonatomic, retain) MBProgressHUDEx *progressHub;

@property (nonatomic, retain) UIButton *favouriteButton;
@property (nonatomic, assign) BOOL hasFavourited;

@end

@implementation RCRecommendFoodDetailViewController

@synthesize dataModel = _dataModel;
@synthesize foodID = _foodID;
@synthesize progressHub = _progressHub;
@synthesize requestID = _requestID;
@synthesize favouriteButton = _favouriteButton;
@synthesize favouriterRequestID = _favouriterRequestID;
@synthesize cancelRequestID = _cancelRequestID;

- (id) initWithFoodID:(NSString *)Mid
{
    self = [super init];
    if(self){
        self.foodID = Mid;
        self.hasFavourited = NO;
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void) dealloc
{
    self.foodID = nil;
    self.dataModel = nil;
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    self.favouriteButton = nil;
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self refreshData];
    self.titleLabelText = @"美食详情页";
    self.navigationItem.titleView = [self createTitleView];
    
    
    
    CGRect rect = CGRectMake(0, 0, 320, UIScreenHeight-64-46);
    _tableView = [[UITableView alloc] initWithFrame:rect];
    [self.viewContainer addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView release];
    
    NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
    
    
    if (!_favouriteButton) {
        _favouriteButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 0, 44, 44)];
        _favouriteButton.backgroundColor = [UIColor clearColor];
        NSString *sharePath = [resourcePath stringByAppendingPathComponent:kFavouriteImageFile];
        [_favouriteButton setImage:[UIImage imageWithContentsOfFile:sharePath] forState:UIControlStateNormal];
        [_favouriteButton setImage:[UIImage imageWithContentsOfFile:sharePath] forState:UIControlStateSelected];
        [_favouriteButton addTarget:self action:@selector(favouriteButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.navigationItem.rightBarButtonItem = [self createRightBarButtonItemWithButton:_favouriteButton];
    
    UIImageView *bottomBGImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, rect.size.height, 320, 46)];
    bottomBGImageView.backgroundColor = [UIColor clearColor];
    NSString *bottomPath = [resourcePath stringByAppendingPathComponent:kBottomBarImageFile];
    [bottomBGImageView setImage:[UIImage imageWithContentsOfFile:bottomPath]];
    
    bottomBGImageView.userInteractionEnabled = YES;
    
    UIButton *signButton = [[UIButton alloc]initWithFrame:CGRectMake(22, 4, 20, 20)];
    signButton.backgroundColor = [UIColor clearColor];
    NSString *signPath = [resourcePath stringByAppendingPathComponent:kSignImageFile];
    [signButton setImage:[UIImage imageWithContentsOfFile:signPath] forState:UIControlStateNormal];
    [signButton setImage:[UIImage imageWithContentsOfFile:signPath] forState:UIControlStateHighlighted];
    [bottomBGImageView addSubview:signButton];
    [signButton release];
    
    UILabel *signLabel = [[UILabel alloc]initWithFrame:CGRectMake(13, 27, 50, 11)];
    signLabel.backgroundColor = [UIColor clearColor];
    signLabel.textColor = [UIColor colorWithHex:0xff3f3f3f];
    signLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:10];
    signLabel.text = @"签到留言";
    [bottomBGImageView addSubview:signLabel];
    [signLabel release];
    
    UIButton *signTransButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 46)];
    signTransButton.backgroundColor = [UIColor clearColor];
    [signTransButton addTarget:self action:@selector(signButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [bottomBGImageView addSubview:signTransButton];
    [signTransButton release];
    
    
    UIButton *sendPhotoButton = [[UIButton alloc]initWithFrame:CGRectMake(112, 4, 20, 20)];
    sendPhotoButton.backgroundColor = [UIColor clearColor];
    NSString *sendPhotoPath = [resourcePath stringByAppendingPathComponent:kSendPhotoImageFile];
    [sendPhotoButton setImage:[UIImage imageWithContentsOfFile:sendPhotoPath] forState:UIControlStateNormal];
    [sendPhotoButton setImage:[UIImage imageWithContentsOfFile:sendPhotoPath] forState:UIControlStateHighlighted];
    [bottomBGImageView addSubview:sendPhotoButton];
    [sendPhotoButton release];
    
    UILabel *sendPhotoLabel = [[UILabel alloc]initWithFrame:CGRectMake(101, 27, 50, 11)];
    sendPhotoLabel.backgroundColor = [UIColor clearColor];
    sendPhotoLabel.textColor = [UIColor colorWithHex:0xff3f3f3f];
    sendPhotoLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:10];
    sendPhotoLabel.text = @"上传照片";
    [bottomBGImageView addSubview:sendPhotoLabel];
    [sendPhotoLabel release];
    
    UIButton *sendPhotoTransButton = [[UIButton alloc]initWithFrame:CGRectMake(90, 0, 70, 46)];
    sendPhotoTransButton.backgroundColor = [UIColor clearColor];
    [sendPhotoTransButton addTarget:self action:@selector(sendPhotoButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [bottomBGImageView addSubview:sendPhotoTransButton];
    [sendPhotoTransButton release];
    
    
    UIButton *dianpingButton = [[UIButton alloc]initWithFrame:CGRectMake(198, 4, 20, 20)];
    dianpingButton.backgroundColor = [UIColor clearColor];
    NSString *dianpingPath = [resourcePath stringByAppendingPathComponent:kDianpingImageFile];
    [dianpingButton setImage:[UIImage imageWithContentsOfFile:dianpingPath] forState:UIControlStateNormal];
    [dianpingButton setImage:[UIImage imageWithContentsOfFile:dianpingPath] forState:UIControlStateHighlighted];
    [bottomBGImageView addSubview:dianpingButton];
    [dianpingButton release];
    
    UILabel *dianpingLabel = [[UILabel alloc]initWithFrame:CGRectMake(187, 27, 50, 11)];
    dianpingLabel.backgroundColor = [UIColor clearColor];
    dianpingLabel.textColor = [UIColor colorWithHex:0xff3f3f3f];
    dianpingLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:10];
    dianpingLabel.text = @"添加点评";
    [bottomBGImageView addSubview:dianpingLabel];
    [dianpingLabel release];
    
    UIButton *dianpingTransButton = [[UIButton alloc]initWithFrame:CGRectMake(170, 0, 70, 46)];
    dianpingTransButton.backgroundColor = [UIColor clearColor];
    [dianpingTransButton addTarget:self action:@selector(dianpingButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [bottomBGImageView addSubview:dianpingTransButton];
    [dianpingTransButton release];
    
    
    UIButton *wrongButton = [[UIButton alloc]initWithFrame:CGRectMake(280, 4, 20, 20)];
    wrongButton.backgroundColor = [UIColor clearColor];
    NSString *wrongPath = [resourcePath stringByAppendingPathComponent:kWrongImageFile];
    [wrongButton setImage:[UIImage imageWithContentsOfFile:wrongPath] forState:UIControlStateNormal];
    [wrongButton setImage:[UIImage imageWithContentsOfFile:wrongPath] forState:UIControlStateHighlighted];
    [bottomBGImageView addSubview:wrongButton];
    [wrongButton release];
    
    UILabel *wrongLabel = [[UILabel alloc]initWithFrame:CGRectMake(279, 27, 50, 11)];
    wrongLabel.backgroundColor = [UIColor clearColor];
    wrongLabel.textColor = [UIColor colorWithHex:0xff3f3f3f];
    wrongLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:10];
    wrongLabel.text = @"报错";
    [bottomBGImageView addSubview:wrongLabel];
    [wrongLabel release];
    
    UIButton *wrongTransButton = [[UIButton alloc]initWithFrame:CGRectMake(250, 0, 70, 46)];
    wrongTransButton.backgroundColor = [UIColor clearColor];
    [wrongTransButton addTarget:self action:@selector(wrongButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [bottomBGImageView addSubview:wrongTransButton];
    [wrongTransButton release];
    
    [self.viewContainer addSubview:bottomBGImageView];
    [bottomBGImageView release];
    
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dianpingButtonClicked{
    
    if (![[GloableEngineService sharedInstance]alertLoginViewIfUnlogin:self]) {
        return;
    }
    
    RCMerchantDetailWriteCommentViewController *vc = [[RCMerchantDetailWriteCommentViewController alloc]initWithMerchant:self.foodID withName:self.dataModel.shopName];
    vc.delegate = (id<MerchantWriteCommentDelegate>)self;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

-(void)signButtonClicked{
    
    if (![[GloableEngineService sharedInstance]alertLoginViewIfUnlogin:self]) {
        return;
    }
    
    RCMerchantDetailWriteSigninViewController *vc = [[RCMerchantDetailWriteSigninViewController alloc]initWithMerchant:self.dataModel.shopID withName:self.dataModel.shopName];
    vc.delegate = (id<MerchantWriteSigninDelegate>)self;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

-(void)wrongButtonClicked{
    
    if (![[GloableEngineService sharedInstance]alertLoginViewIfUnlogin:self]) {
        return;
    }
    RCMerchantCommitWrongInfoViewController *vc = [[RCMerchantCommitWrongInfoViewController alloc]initWithMerchantName:self.dataModel.shopID withName:self.dataModel.shopName];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}


-(void)sendPhotoButtonClicked{
    if (![[GloableEngineService sharedInstance]alertLoginViewIfUnlogin:self]) {
        return;
    }
    
    RCMerchantUploadPhotoViewController *vc = [[RCMerchantUploadPhotoViewController alloc]initWithShopID:self.dataModel.shopID];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}


-(void)bottomButtonClicked{
    RCWaitingMoreVC *waitmoreVC = [[RCWaitingMoreVC alloc]init];
    [self.navigationController pushViewController:waitmoreVC animated:YES];
    [waitmoreVC release];
}

-(void)shareButtonClicked{
    RCWaitingMoreVC *waitmoreVC = [[RCWaitingMoreVC alloc]init];
    [self.navigationController pushViewController:waitmoreVC animated:YES];
    [waitmoreVC release];
}

-(void)favouriteButtonClicked{
    if (![[GloableEngineService sharedInstance]alertLoginViewIfUnlogin:self]) {
        return;
    }
    
    
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    
    if (self.hasFavourited) {
        _cancelRequestID = [[IManager sharedInstance]requestMerchantDeleteFavourite:self.foodID withDelegate:self];
    }else{
        _favouriterRequestID = [[IManager sharedInstance]requestMerchantAddFavourite:self.foodID withDelegate:self];
    }
}


#pragma
#pragma mark --- UITableView

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (nil == self.dataModel || nil == self.dataModel.foodDetailItems || [self.dataModel.foodDetailItems count] == 0) {
        return 0;
    }
    if ([self.dataModel.foodLat doubleValue] == 0.0f || [self.dataModel.foodLon doubleValue] == 0.0f) {
        return [self.dataModel.foodDetailItems count] +1;
    }
    return [self.dataModel.foodDetailItems count] +2;
}


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == 0) {
        return 110;
    }else{
        
        RCMerchantDetailItem *item = nil;
        if ([indexPath row] < [self.dataModel.foodDetailItems count] +1) {
            item = [self.dataModel.foodDetailItems objectAtIndex:([indexPath row]-1)];
        }
        
        if (item.type == kMerchantDetailType_Address || item.type == kMerchantDetailType_Phone|| item.type == kMerchantDetailType_Order|| item.type == kMerchantDetailType_OrderServer|| item.type == kMerchantDetailType_Traffic|| item.type == kMerchantDetailType_Othershop) {
            
            return [RCMerchantDetailOnelineCell cellHeightWithString:item];
        }else if (item.type == kMerchantDetailType_Preferential){
            return [RCMerchantDetailTwolineCell cellHeightWithString:item.content];
        }else if (item.type == kMerchantDetailType_Recommended){
            return [RCMerchantDetailRecommendCell cellHeightWithString:item.content];
        }else if (item.type == kMerchantDetailType_Review){
            return [RCMerchantDetailCommentCell cellHeightWithString:item.content];
        }else if (item.type == kMerchantDetailType_Signin){
            return [RCMerchantDetailSignCell cellHeight];
        }else if (item.type == kMerchantDetailType_Food){
            return [RCMerchantDiscoutCell getCellHeight:item];
            
        }else if ([indexPath row] == [self.dataModel.foodDetailItems count] +1){
            if ([self.dataModel.foodLat doubleValue] == 0.0f || [self.dataModel.foodLon doubleValue] == 0.0f) {
                return 0.0f;
            }
            return [RCMerchantDetailPeripheryCell cellHeight];
        }else{
            return [RCMerchantDetailTwolineCell cellHeightWithString:item.content];
        }
        return 60;
    }
}


- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if ([indexPath row] == 0) {
        RCRecommendFoodDetailHeaderCell *cell = [[RCRecommendFoodDetailHeaderCell alloc] init];
        cell.delegate = self;
        [cell bindData:self.dataModel];
        return [cell autorelease];
    }
    RCMerchantDetailItem *item = nil;
    if ([indexPath row] < [self.dataModel.foodDetailItems count] +1) {
        item = [self.dataModel.foodDetailItems objectAtIndex:([indexPath row]-1)];
    }
    if (item.type == kMerchantDetailType_Address) {
        RCMerchantDetailOnelineCell *oneLineCell = [[RCMerchantDetailOnelineCell alloc] init];
        oneLineCell.cellType = OneLineCellTypeAddress;
        oneLineCell.parentViewController = self;
        oneLineCell.merchantName = self.dataModel.shopName;
        CLLocationCoordinate2D location = CLLocationCoordinate2DMake([self.dataModel.foodLat doubleValue], [self.dataModel.foodLon doubleValue]);
        oneLineCell.location = location;
        [oneLineCell bindData:item];
        return [oneLineCell autorelease];
    }else if (item.type == kMerchantDetailType_Phone) {
        RCMerchantDetailOnelineCell *oneLineCell = [[RCMerchantDetailOnelineCell alloc] init];
        oneLineCell.cellType = OneLineCellTypePhone;
        oneLineCell.parentViewController = self;
        [oneLineCell bindData:item];
        return [oneLineCell autorelease];
    }else if (item.type == kMerchantDetailType_Order) {
        RCMerchantDetailOnelineCell *oneLineCell = [[RCMerchantDetailOnelineCell alloc] init];
        oneLineCell.parentViewController = self;
        oneLineCell.merchantID = self.dataModel.shopID;
        oneLineCell.cellType = OneLineCellTypeOrder;
        [oneLineCell bindData:item];
        return [oneLineCell autorelease];
    }else if (item.type == kMerchantDetailType_OrderServer) {
        RCMerchantDetailOnelineCell *oneLineCell = [[RCMerchantDetailOnelineCell alloc] init];
        oneLineCell.parentViewController = self;
        oneLineCell.merchantID = self.dataModel.shopID;
        oneLineCell.cellType = OneLineCellTypeOrderServe;
        [oneLineCell bindData:item];
        return [oneLineCell autorelease];
    }else if (item.type == kMerchantDetailType_Traffic) {
        
        RCMerchantDetailOnelineCell *oneLineCell = [[RCMerchantDetailOnelineCell alloc] init];
        oneLineCell.parentViewController = self;
        oneLineCell.merchantID = self.dataModel.shopID;
        oneLineCell.cellType = OneLineCellTypeTraffic;
        UIView *backgroundBGView = [[[UIView alloc]initWithFrame:oneLineCell.bounds]autorelease];
        backgroundBGView.backgroundColor = [UIColor colorWithHex:0xffe7e7e7];
        oneLineCell.selectedBackgroundView = backgroundBGView;
        [oneLineCell bindData:item];
        return [oneLineCell autorelease];
        
    }else if (item.type == kMerchantDetailType_Othershop) {
        RCMerchantDetailOnelineCell *oneLineCell = [[RCMerchantDetailOnelineCell alloc] init];
        oneLineCell.parentViewController = self;
        oneLineCell.merchantID = self.dataModel.shopID;
        oneLineCell.cellType = OneLineCellTypeOtherShop;
        UIView *backgroundBGView = [[[UIView alloc]initWithFrame:oneLineCell.bounds]autorelease];
        backgroundBGView.backgroundColor = [UIColor colorWithHex:0xffe7e7e7];
        oneLineCell.selectedBackgroundView = backgroundBGView;
        [oneLineCell bindData:item];
        [oneLineCell bindData:item];
        return [oneLineCell autorelease];
    }else if (item.type == kMerchantDetailType_Recommended) {
        RCMerchantDetailRecommendCell *recommendCell = [[RCMerchantDetailRecommendCell alloc]init];
        [recommendCell bindData:item];
        return [recommendCell autorelease];
    }else if (item.type == kMerchantDetailType_Review) {
        RCMerchantDetailCommentCell *commentCell = [[RCMerchantDetailCommentCell alloc]init];
        [commentCell bindData:item];
        UIView *backgroundBGView = [[[UIView alloc]initWithFrame:commentCell.bounds]autorelease];
        backgroundBGView.backgroundColor = [UIColor colorWithHex:0xffe7e7e7];
        commentCell.selectedBackgroundView = backgroundBGView;
        return [commentCell autorelease];
    }else if (item.type == kMerchantDetailType_Signin) {
        RCMerchantDetailSignCell *signinCell = [[RCMerchantDetailSignCell alloc]init];
        [signinCell bindData:item];
        UIView *backgroundBGView = [[[UIView alloc]initWithFrame:signinCell.bounds]autorelease];
        backgroundBGView.backgroundColor = [UIColor colorWithHex:0xffe7e7e7];
        signinCell.selectedBackgroundView = backgroundBGView;
        return [signinCell autorelease];
        
    }else if (item.type == kMerchantDetailType_Food) {
        RCMerchantDiscoutCell *discountCell = [[RCMerchantDiscoutCell alloc]init];
        [discountCell bindData:item];
        return [discountCell autorelease];
        
    }else if ([indexPath row] == [self.dataModel.foodDetailItems count] +1) {
        RCMerchantDetailPeripheryCell *peripheryCell = [[RCMerchantDetailPeripheryCell alloc]init];
        peripheryCell.delegate = self;
        return [peripheryCell autorelease];
    }else {
        RCMerchantDetailTwolineCell *twoLineCell = [[RCMerchantDetailTwolineCell alloc] init];
        [twoLineCell bindData:item];
        return [twoLineCell autorelease];
    }
    return nil;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if ([indexPath row] == 0) {
        return;
    }
    
    RCMerchantDetailItem *item = nil;
    if ([indexPath row] < [self.dataModel.foodDetailItems count] +1) {
        item = [self.dataModel.foodDetailItems objectAtIndex:([indexPath row]-1)];
    }
    
    if (item.type == kMerchantDetailType_Review) {
        NetworkStatus status = [GloableEngineService sharedInstance].netWorkStatus;
        if (status == kNotReachable) {
            [RCAppDelegate alertWithNetworkError];
            return;
        }
        RCMerchantCommentListViewController *vc = [[RCMerchantCommentListViewController alloc]initWithShopId:self.dataModel.shopID withShopName:self.dataModel.shopName];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }else if (item.type == kMerchantDetailType_Traffic){
        NetworkStatus status = [GloableEngineService sharedInstance].netWorkStatus;
        if (status == kNotReachable) {
            [RCAppDelegate alertWithNetworkError];
            return;
        }
        RCMerchantDetailTrafficViewController *vc = [[RCMerchantDetailTrafficViewController alloc]initWithShopID:self.dataModel.shopID withShopName:self.dataModel.shopName];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }else if (item.type == kMerchantDetailType_Othershop){
        NetworkStatus status = [GloableEngineService sharedInstance].netWorkStatus;
        if (status == kNotReachable) {
            [RCAppDelegate alertWithNetworkError];
            return;
        }
        RCMerchantDetailOtherShopViewController *vc = [[RCMerchantDetailOtherShopViewController alloc]initWithShopID:self.dataModel.shopID withLat:self.dataModel.foodLat withLon:self.dataModel.foodLon];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }else if (item.type == kMerchantDetailType_Signin){
        NetworkStatus status = [GloableEngineService sharedInstance].netWorkStatus;
        if (status == kNotReachable) {
            [RCAppDelegate alertWithNetworkError];
            return;
        }
        RCMerchantDetailSigninListViewController *vc = [[RCMerchantDetailSigninListViewController alloc]initWithShopId:self.dataModel.shopID withShopName:self.dataModel.shopName];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }
}
#pragma
#pragma mark ServerData ----下拉刷新数据

-(void) refreshData
{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    _requestID = [[IManager sharedInstance] requestRecommendFoodDetail:self.foodID withDelegate:self];
}



#pragma
#pragma mark IManage Delegate
-(void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        
        self.dataModel = (RCRecommendFoodDetailDataModel *)dataModel;
        
        self.hasFavourited = self.dataModel.favourited;
        NSString *filePath = nil;
        
        if (self.hasFavourited) {
            filePath = [[NSBundle mainBundle] pathForResource:kFavouritedImageFile ofType:nil];
            [_favouriteButton setImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateNormal];
            [_favouriteButton setImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateSelected];
        }else{
            filePath = [[NSBundle mainBundle] pathForResource:kFavouriteImageFile ofType:nil];
            [_favouriteButton setImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateNormal];
            [_favouriteButton setImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateSelected];
        }
        
        [_tableView reloadData];
    }else if (_favouriterRequestID == requestId){
        
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        
        [RCAppDelegate alertWithCustomizeString:@"收藏成功"];
        self.hasFavourited = YES;
        NSString *filePath = [[NSBundle mainBundle] pathForResource:kFavouritedImageFile ofType:nil];
        [_favouriteButton setImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateNormal];
        [_favouriteButton setImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateSelected];
    }else if (_cancelRequestID == requestId){
        
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        
        [RCAppDelegate alertWithCustomizeString:@"取消收藏成功"];
        self.hasFavourited = NO;
        NSString *filePath = [[NSBundle mainBundle] pathForResource:kFavouriteImageFile ofType:nil];
        [_favouriteButton setImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateNormal];
        [_favouriteButton setImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateSelected];
    }
    
    
}

-(void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
    }else if (_favouriterRequestID == requestId){
        
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        
        [RCAppDelegate alertWithCustomizeString:@"收藏失败"];
        
    }else if (_cancelRequestID == requestId){
        
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        
        [RCAppDelegate alertWithCustomizeString:@"取消收藏失败"];
        
    }
}

#pragma WriteSignin and Comment Delegate

-(void)successWriteComment{
    [self refreshData];
}

-(void)successWriteSignin{
    [self refreshData];
}

-(void)gotoMerchantNearbyWithTag:(NSInteger)tag{
    NSString *title = nil;
    if (tag == 1)             title = @"美食";
    else if (tag == 2)    title = @"周边景点";
    else if (tag == 3)    title = @"旅游酒店";
    else if (tag == 4)    title = @"休闲娱乐";
    
    CLLocation *location = [[CLLocation alloc]initWithLatitude:[self.dataModel.foodLat doubleValue] longitude:[self.dataModel.foodLon doubleValue]];
    RCMerchantListVC *list = [[RCMerchantListVC alloc] initWithMerchantType:title withMerchantID:nil];
    list.merchantLocation = location;
    list.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:list animated:YES];
    [list release];
    [location release];
}

-(void)gotoFoodPictureAlbumView{
    if ([GloableEngineService sharedInstance].netWorkStatus == kNotReachable) {
        [RCAppDelegate alertWithNetworkError];
        return;
    }
    RCMerchantPictureViewController *vc = [[RCMerchantPictureViewController alloc]initWithShopID:self.dataModel.shopID andShopName:self.dataModel.shopName];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

@end
